package com.atlbb.controller;

import com.atlbb.mybatis.entity.Manager;
import com.atlbb.service.ManagerService;
import com.atlbb.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LiangQingBin
 * @version 1.00
 * @ClassName ManagerController
 * @Description TODO
 * @time 2021/11/11 19:37
 */
@Controller
@Slf4j
@CrossOrigin
public class ManagerController {
    @Autowired
    ManagerService managerService;
    
    public static boolean isLogin=false;
    
    @GetMapping("/unLogin")
    @ResponseBody
    public String unLogin(HttpSession session){
        isLogin=false;
        return "退出登录成功！";
    }
    
    @ResponseBody
    @PostMapping("/login")
    public Map<String,Object> login(@RequestBody Manager manager, HttpServletRequest request, HttpSession session) {
        HashMap<String, Object> results = new HashMap<>();
        //客户端传来的数据
        String managerName = manager.getManagerName();
        String managerPassword = manager.getManagerPassword();
//        System.out.println("方法到这里了！");
        //判断用户是否存在
        if (managerService.findOneManagerByManagerName(managerName)== null){
            results.put("msg","没有该用户!");
            return results;
        }else if (managerService.findOneManagerByManagerName(managerName).getManagerPassword().equals(managerPassword)){
            //登录成功
            session.setAttribute("hadLogin",manager);
            isLogin = true;
            results.put("msg","登录成功!");
            return results;
        }
        results.put("msg","用户名或密码错误!");
        return results;
    }
    
}
